﻿namespace Sidvall.Serialization
{
	public static class NumberExtension
	{
		#region Add

		public static double? Add(this double? item, double? value, bool ignoreNull)
		{
			if (ignoreNull)
			{
				if (item != null)
				{
					if (value != null)
						return item + value;
					else
						return item;
				}
				if (value != null)
					return value;
				return null;
			}
			else
			{
				return item + value;
			}
		}

		#endregion
		#region IsBetween

		public static bool IsBetween(this double value, double? minValue, double? maxValue, bool minInclusive, bool maxInclusive)
		{
			if (minValue != null)
			{
				if (maxValue != null)
				{
					if (((minInclusive) && (minValue.Value <= value)) || ((!minInclusive) && (minValue.Value < value)))
					{
						if (((maxInclusive) && (maxValue.Value >= value)) || ((!maxInclusive) && (maxValue.Value > value)))
							return true;
					}
				}
				else
				{
					if (((minInclusive) && (minValue.Value <= value)) || ((!minInclusive) && (minValue.Value < value)))
						return true;
				}
			}
			else if (maxValue != null)
			{
				if (((maxInclusive) && (maxValue.Value >= value)) || ((!maxInclusive) && (maxValue.Value > value)))
					return true;
			}

			return false;
		}

		#endregion
	}
}
